#include<jni.h>
#include<pthread.h>
#undef __cplusplus
jclass g_clazz;
JavaVM* jvm;
JNI_OnLoad(JavaVM* _jvm, void* _reserved){
	jvm = _jvm;
	JNIEnv* env;
	  if ((*_jvm)->GetEnv(_jvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
	    return -1;
	  jclass clazz = (*env)->FindClass(env,"com/framework/ndk/Seraph");
	  g_clazz = (*env)->NewGlobalRef(env, clazz);
	jint result = JNI_VERSION_1_6;
	return result;
}
static void* test_event_fun(void* arg){
	int i = 0;
	while(1){
		sleep(2);
		if(i++ > 10) i =10;
		uusee_callback(i);
	}
}

int uusee_callback(int event){
	JNIEnv* jenv_static ;
	jclass clazz =NULL;
	int result = (*jvm)->AttachCurrentThread(jvm, (void**)&jenv_static, NULL);
	if(result < 0){
		return -1;
	}

	if(g_clazz == NULL){
		(*jvm)->DetachCurrentThread(jvm);
		return -2;
	}

	jmethodID id = (*jenv_static)->GetStaticMethodID(jenv_static,g_clazz,"Printf","(I)V");
	if(id==NULL){
		(*jvm)->DetachCurrentThread(jvm);
		return -3;
	}
	(*jenv_static)->CallStaticVoidMethod(jenv_static,id,2);

	return 0;
}
JNIEXPORT void JNICALL Java_com_framework_ndk_Seraph_start
  (JNIEnv *env, jobject obj){
	jclass clazz = (*env)->FindClass(env,"com/framework/ndk/Seraph");
	jmethodID id = (*env)->GetStaticMethodID(env,clazz,"Printf","(I)V");
	(*env)->CallStaticVoidMethod(env,id,2);
//	pthread_t tid;
//	pthread_create(&tid,NULL,test_event_fun,NULL);
//
}

/*
 * Class:     com_framework_ndk_Seraph
 * Method:    stop
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_framework_ndk_Seraph_stop
  (JNIEnv *env, jobject obj){

}
